package com.yuexunit.finance.system.action.v1;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;

import com.google.inject.Singleton;
import com.yuexunit.core.ConfigPropertites;
import com.yuexunit.fs.domain.FileEntry;
import com.yuexunit.fs.service.TransferService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.web.annotation.ActionServlet;

/**
*@version 创建时间：2017年6月27日 下午2:29:56
*@author wenwenlong
*类说明
*/

@ActionServlet({"/api/[^/]+/getDocPicByPage.json"})
@Singleton
public class GetDocPicByPageAction extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	
	private TransferService transferService = ComponentFactory.loadClass(TransferService.class);

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		try {
			String fileUuid = request.getParameter("fileUuid");
			Integer page = Integer.valueOf(request.getParameter("page"));
			
			FileEntry fe = transferService.inquireFileEntry(fileUuid);
			File file = new File(ConfigPropertites.getProperties("UPLOAD_FILE_PATH") + fe.getRelativePath());
			
			PDDocument doc = PDDocument.load(file);
			PDFRenderer renderer = new PDFRenderer(doc);
			BufferedImage image = renderer.renderImageWithDPI(page-1, 300, ImageType.RGB); // Windows
			
			BufferedImage srcImage = resize(image, 1240, 1750);// 产生缩略图
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
			ImageIO.write(srcImage, "png", outputStream);  
			
			response.setContentType("image/png");
			OutputStream os = response.getOutputStream();  
	        os.write(outputStream.toByteArray());
	        
	        os.close();
	        outputStream.close();
	        doc.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		this.doPost(request, response);
	}

	private BufferedImage resize(BufferedImage source, int targetW, int targetH) {
		int type = source.getType();
		BufferedImage target = null;
		double sx = (double) targetW / source.getWidth();
		double sy = (double) targetH / source.getHeight();
		if (sx > sy) {
			sx = sy;
			targetW = (int) (sx * source.getWidth());
		} else {
			sy = sx;
			targetH = (int) (sy * source.getHeight());
		}
		if (type == BufferedImage.TYPE_CUSTOM) {
			ColorModel cm = source.getColorModel();
			WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
			boolean alphaPremultiplied = cm.isAlphaPremultiplied();
			target = new BufferedImage(cm, raster, alphaPremultiplied, null);
		} else {
			target = new BufferedImage(targetW, targetH, type);
		}
		Graphics2D g = target.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
		g.dispose();
		return target;
	}
}
